package com.zrf.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.zrf.hibernate.model.MyShoppingCart;
import com.zrf.hibernate.pojo.Buyer;
import com.zrf.hibernate.pojo.Product;
import com.zrf.hibernate.pojo.Shoppingcart;
import com.zrf.tool.Tools;


@Repository("shoppingcartDao")
public class ShoppingcartDaoImpl extends BaseDaoImpl<Shoppingcart>{

	private Session session = null;
	private Query query = null;
	private Transaction transaction = null;
	private Tools tool = null;
	
	
	public boolean addShoppingCart(Buyer b,Product p,Integer num){
		boolean result = false;
		List<?> list = null;
		Shoppingcart sc = null;
		String hql = "";
		//商品缺货
		if(p.getPnum()<num){
			return result;
		}
		//查购物车，如果有增减，如果没有新建
		try {
			hql = "FROM Shoppingcart sc WHERE sc.product.productid=?";
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			transaction = session.beginTransaction();
			query = session.createQuery(hql);
			query.setInteger(0, p.getProductid());
			list = query.list();
			
			if(list!=null && list.size()>0){
				sc = (Shoppingcart) list.get(0);
				p.setPnum(p.getPnum()-num);
				sc.setNum(sc.getNum()+num);
				session.update(sc);
				session.update(p);
			}else{
				hql = "SELECT sc.scartid FROM Shoppingcart sc";
				query = session.createQuery(hql);
				list = query.list();
				Integer id = Tools.getListID(list);
				sc = new Shoppingcart(id, p, b, num);
				p.setPnum(p.getPnum()-num);
				session.save(sc);
				session.update(p);
			}
			transaction.commit();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
		if(session!=null && session.isOpen()){
			session.close();
		}
		return result;
	}
	
	
	
	public void deleteShoppingCartByBuyerName(String name){
		String hql = "DELETE FROM Shoppingcart sc WHERE sc.buyer.bname=?";
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			query = session.createQuery(hql);
			query.setString(0, name);
			query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		if(session!=null && session.isOpen()){
			session.close();
		}
	}
	
	
	public List<Shoppingcart> findByBuyerName(String name){
		List<?> list = null;
		String hql = "FROM Shoppingcart sc WHERE sc.buyer.bname=?";
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			query = session.createQuery(hql);
			list = query.list();
			if(list!=null && list.size()<=0){
				list = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		if(session!=null && session.isOpen()){
			session.close();
		}
		
		return (List<Shoppingcart>) list;
	}
	
	
	
	public List<MyShoppingCart> findByName(String name){
		List<?> list  = null;
		String hql = "SELECT NEW com.zrf.hibernate.model.MyShoppingCart"
				+ "(sc.scartid,sc.product.pname,sc.product.pprice,sc.num,sc.product.pprice*sc.num)"
				+ "FROM Shoppingcart sc WHERE sc.buyer.bname=?";
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			query = session.createQuery(hql);
			query.setString(0, name);
			list = query.list();
			if(list!=null && list.size()<=0){
				list = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		if(session!=null && session.isOpen()){
			session.close();
		}
		
		return (List<MyShoppingCart>) list;
	}
	
	
	
	
	
}
